/*
 *  WsvJdbc - a JDBC driver for WSV (values separated by whatever) data files
 *  Copyright (C) 2004  Daniele Pes
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package axs.jdbc.dataSpecification.supportedJavaTypes;

import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;


/**
 * This class is useful to get typed data out of relating <code>String</code> values.
 * 
 * @author 		Daniele Pes
 * @version    $Id: TypesMapper.java,v 1.2 2007/04/16 17:24:36 montag451 Exp $
 */
public class TypesMapper {

	private static HashMap typesMap;
	private static HashMap primitiveTypesToWrappersMap;

	private static int numOfTypes;
	private static int i;
	private static Object keyClassObj;
	private static Object valueClassObj;


	public static Class[] types = {
		String.class,
		Boolean.class,
		Byte.class,
		Character.class,
		Double.class,
		Float.class,
		Integer.class,
		Long.class,
		Short.class,
		Date.class,
		boolean.class,
		byte.class,
		char.class,
		double.class,
		float.class,
		int.class,
		long.class,
		short.class,
		Timestamp.class,
		java.sql.Date.class,
		Time.class		
	};
		
	
	public static TypeClass[] classTypes = {
		new StringClass(),
		new BooleanClass(),
		new ByteClass(),
		new CharacterClass(),
		new DoubleClass(),
		new FloatClass(),
		new IntegerClass(),
		new LongClass(),
		new ShortClass(),
		new DateClass(),
		new PbooleanClass(),
		new PbyteClass(),
		new PcharClass(),
		new PdoubleClass(),
		new PfloatClass(),
		new PintClass(),
		new PlongClass(),
		new PshortClass(),
		new SqlTimeStampClass(),
		new SqlDateClass(),
		new SqlTimeClass()
	};

	
	public static boolean isAtimeFormattedType(TypeClass type){
		return (type instanceof DateClass ||
				  type instanceof SqlTimeStampClass ||
				  type instanceof SqlDateClass ||
				  type instanceof SqlTimeClass);
	}
	
	
	/**
	 * If type is of the <code>java.util.Date</code> family, then the standard JDBC format will be used,
	 * unless not specified another time format through available time TypeClasses methods.
	 * @param typeClass
	 * @return
	 */
	public static TypeClass getTypeClass(Class typeClass) {		
		return (TypeClass)typesMap.get(typeClass);
	}
	
	
	public static Object getWrapperFromPrimitive(Class primitive){
		return primitiveTypesToWrappersMap.get(primitive);
	}
	

	public static Class[] primitiveTypes = {
		int.class,		
		float.class,
		boolean.class,
		byte.class,
		char.class,
		double.class,				
		long.class,
		short.class
	};


	public static Object[] wrapperTypes = {
		new Integer(0),		
		new Float(0.0),
		new Boolean(false),
		new Byte("0"),
		new Character('0'),
		new Double(0.0),				
		new Long(0),
		new Short("0")
	};


	public static final int INT 	  = 0;
	public static final int FLOAT   = 1;
	public static final int BOOLEAN = 2;
	public static final int BYTE    = 3;
	public static final int CHAR    = 4;
	public static final int DOUBLE  = 5;
	public static final int LONG 	  = 6;
	public static final int SHORT   = 7;


	public TypesMapper(){
		int numOfTypes = types.length;
		//
		for (int i = 0; i<numOfTypes; i++){
			typesMap.put(types[i], classTypes[i]);
		}
	}
	
	
	// STATIC INITIALIZATION
	static{
		numOfTypes = types.length;
		typesMap = new HashMap();
		//initialize ClassMap vector				
		for (i = 0; i<numOfTypes; i++){
			keyClassObj = types[i];
			valueClassObj = classTypes[i];
			typesMap.put(keyClassObj, valueClassObj);
		}
		//initialize WrappersMap vector
		numOfTypes = primitiveTypes.length;
		primitiveTypesToWrappersMap = new HashMap();
		for (i = 0; i<numOfTypes; i++){
			keyClassObj = primitiveTypes[i];
			valueClassObj = wrapperTypes[i];
			primitiveTypesToWrappersMap.put(keyClassObj, valueClassObj);
		}
	}
	
	
	public static boolean isSimpleType(Class type){
		return (
			type.isPrimitive() 
			||(
				type.equals(String.class) ||
				type.equals(Boolean.class) ||
				type.equals(Byte.class) ||
				type.equals(Character.class) ||
				type.equals(Double.class) ||
				type.equals(Float.class) ||
				type.equals(Integer.class) ||
				type.equals(Long.class) ||
				type.equals(Short.class) 
			) 
			|| isAtimeType(type)
		);
	}


	/**
	 * @param type
	 * @return
	 */
	private static boolean isAtimeType(Class type) {		
		return 
			type.equals(Date.class) ||
			type.equals(java.sql.Date.class) ||
			type.equals(Time.class) ||
			type.equals(Timestamp.class);
	}

}
